package org.dromara.system.controller;

import java.util.List;
import java.util.Set;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.core.utils.BeanUtils;
import org.dromara.common.core.validate.QueryGroup;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.vo.PayAccountSelectVo;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.system.domain.vo.PayAccountVo;
import org.dromara.system.domain.bo.PayAccountBo;
import org.dromara.system.service.IPayAccountService;
import org.dromara.common.mybatis.core.page.TableDataInfo;

/**
 * 收款账号
 * 前端访问路由地址为:/system/PayAccount
 *
 * @author Gavino
 * @date 2024-10-22
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/PayAccount")
public class PayAccountController extends BaseController {

    private final IPayAccountService payAccountService;

    /**
     * 获取这个应用未绑定的收款账号
     */
    @SaCheckPermission("system:PayAccount:list")
    @GetMapping("/noBinds")
    public TableDataInfo<PayAccountVo> noBinds(@Validated(QueryGroup.class) PayAccountBo bo, PageQuery pageQuery) {
        return payAccountService.noBindsList(bo, pageQuery);
    }

    /**
     * 查询收款账号列表
     */
    @SaCheckPermission("system:PayAccount:list")
    @GetMapping("/list")
    public TableDataInfo<PayAccountVo> list(PayAccountBo bo, PageQuery pageQuery) {
        return payAccountService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询收款账号列表
     */
    @GetMapping("/queryList")
    public R<?> queryList(PayAccountBo bo) {
        List<PayAccountVo> list = payAccountService.queryList(bo);
        return R.ok(BeanUtils.copyList(list, PayAccountSelectVo.class));
    }

    /**
     * 导出收款账号列表
     */
    @SaCheckPermission("system:PayAccount:export")
    @Log(title = "收款账号", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(PayAccountBo bo, HttpServletResponse response) {
        List<PayAccountVo> list = payAccountService.queryList(bo);
        ExcelUtil.exportExcel(list, "收款账号", PayAccountVo.class, response);
    }

    /**
     * 获取收款账号详细信息
     *
     * @param accountId 主键
     */
    @SaCheckPermission("system:PayAccount:query")
    @GetMapping("/{accountId}")
    public R<PayAccountVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long accountId) {
        return R.ok(BeanUtils.copy(payAccountService.selectById(accountId), PayAccountVo.class));
    }

    /**
     * 新增收款账号
     */
    @SaCheckPermission("system:PayAccount:add")
    @Log(title = "收款账号", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody PayAccountBo bo) {
        if(!LoginHelper.isAgent() && !LoginHelper.isSupplier()) {
            return R.fail("您当前无权限添加收款账号");
        }
        return toAjax(payAccountService.insertByBo(bo));
    }

    /**
     * 修改收款账号
     */
    @SaCheckPermission("system:PayAccount:edit")
    @Log(title = "收款账号", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PayAccountBo bo) {
        if(!LoginHelper.isSuperAdmin() && !LoginHelper.isAgent() && !LoginHelper.isSupplier()) {
            return R.fail("您当前无权限修改收款账号");
        }
        return toAjax(payAccountService.updateByBo(bo));
    }

    /**
     * 删除收款账号
     *
     * @param accountIds 主键串
     */
    @SaCheckPermission("system:PayAccount:remove")
    @Log(title = "收款账号", businessType = BusinessType.DELETE)
    @DeleteMapping("/{accountIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] accountIds) {
        return toAjax(payAccountService.deleteWithValidByIds(Set.of(accountIds), 1));
    }

    /**
     * 收款账号还原
     *
     * @param accountIds 主键串
     */
    @SaCheckPermission("system:PayAccount:restore")
    @Log(title = "收款账号还原", businessType = BusinessType.OTHER)
    @GetMapping("/restore/{accountIds}")
    public R<Void> restore(@PathVariable Long[] accountIds) {
        return toAjax(payAccountService.deleteWithValidByIds(Set.of(accountIds), 0));
    }

    /**
     * 还原所有收款账号
     *
     */
    @SaCheckPermission("system:PayAccount:restore")
    @Log(title = "还原所有收款账号", businessType = BusinessType.OTHER)
    @GetMapping("/restoreAll")
    public R<Void> restoreAll() {
        return toAjax(payAccountService.deleteWithValidByIds(null, 0));
    }

    /**
     * 彻底删除收款账号
     *
     */
    @SaCheckPermission("system:PayAccount:delete")
    @Log(title = "彻底删除收款账号", businessType = BusinessType.DELETE)
    @GetMapping("/delete/{accountIds}")
    public R<Void> delete(@PathVariable Long[] accountIds) {
        return toAjax(payAccountService.deleteByIds(Set.of(accountIds)));
    }

    /**
     * 彻底删除收款账号
     *
     */
    @SaCheckPermission("system:PayAccount:delete")
    @Log(title = "彻底删除收款账号", businessType = BusinessType.DELETE)
    @GetMapping("/deleteAll")
    public R<Void> deleteAll() {
        return toAjax(payAccountService.deleteByIds(null));
    }

}
